package asionreachel.cn.smarthomedemo.widget.filter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import asionreachel.cn.smarthomedemo.utils.Mlog;


/**
 * Created by Simon(lyd) on 2016/9/27:06.
 * 描述：带有点击效果的imageview
 */

public class FilterImageView extends ImageView{

    public FilterImageView(Context context) {
        this(context,null);
    }

    public FilterImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    private Handler handler;

    public boolean isFilter = true;

    public FilterImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        setSelected(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                /** 设置滤镜 */
                if(isFilter)
                    setFilter();
                break;
            case MotionEvent.ACTION_UP:
                /** 传递点击 */
                performClick();
            case MotionEvent.ACTION_CANCEL:
                /** 删除滤镜 */
                if(isFilter)
                    removeFilter();
                break;
            default:
                break;
        }
        return true;
    }


    /**
     * 设置滤镜
     */
    private void setFilter() {
        //先获取设置的src图片
        Drawable drawable = getDrawable();
        //当src图片为Nul l，获取背景图片
        if (drawable == null) {
            drawable = getBackground();
        }
        if (drawable != null) {
            //设置滤镜
            drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

    /**
     * 清除滤镜
     */
    public void removeFilter() {
        //先获取设置的src图片
        Drawable drawable = getDrawable();
        //当src图片为Null，获取背景图片
        if (drawable == null) {
            drawable = getBackground();
        }
        if (drawable != null) {
            //清除滤镜
            drawable.clearColorFilter();
        }
    }

    public void onStart(){
        if(handler == null){
            handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case 101:
                            FilterImageView.this.setSelected(!FilterImageView.this.isSelected());
                            handler.sendEmptyMessageDelayed(101,1000);
                            break;
                    }
                }
            };
            handler.sendEmptyMessageDelayed(101,1000);
        }
    }

    public void onStop(){
        if(handler!=null) {
            Mlog.e("清除命令-=============");
            this.setSelected(false);
            handler.removeMessages(101);
            handler = null;
        }
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if(onSelectorListener!=null)onSelectorListener.selector(selected);
    }

    private OnSelectorListener onSelectorListener;

    public interface OnSelectorListener{
        void selector(boolean b);
    }

    public void setOnSelectorListener(OnSelectorListener onSelectorListener) {
        this.onSelectorListener = onSelectorListener;
    }
}
